module(...,package.seeall)

------------------------------------------------------
--  Author : Team B
--  Description :create menu scene
------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

-- include Corona's "physics" library
local physics = require( "physics" )
physics.start()
 
-- include Corona's "widget" library
local widget = require "widget"
--------------------------------------------

-- forward declarations and other locals
local singlePlayerBtn
local multiPlayerBtn
local tutorialBtn
local optionBtn
local xPosBtn = display.contentWidth / 4;
local yPosBtn = display.contentHeight - 40;
local widthButton = 140;
local heightButton = 25;
-- Thoi gian dich chuyen button transfrom.from
local timeMoveButton = 200;

-- Thoi gian dich chuyen button transfrom.to
local timeMoveToButton = 200;



-- 'onRelease' event listener for singlePlayer
local function singlePlayerBtnRelease()
	singlePlayerBtn.xScale = 1; singlePlayerBtn.yScale = 1;
	storyboard.gotoScene( "singlePlay", "fade", 100 )
	return true	-- indicates successful touch
 end

-- 'onPress' event listener for playBtn
local function singlePlayerBtnPress( )        
	singlePlayerBtn.xScale = 1.5; 
	singlePlayerBtn.yScale = 1.5;
	return true	-- indicates successful touch
    end
    
-- listenerOnlineBtn 
local listenersinglePlayerBtn = function(obj)
    transition.to( singlePlayerBtn, { time = timeMoveToButton, alpha = 1.0 ,x = xPosBtn, y = yPosBtn - 160} )
end   


-- onRelease event listener for onlineBtn 
local function onMultiPlayerBtnRelease( )
	multiPlayerBtn.xScale = 1; multiPlayerBtn.yScale = 1;
	--
	--local multiPlayerSceene = storyboard.loadScene( "multiPlayer" )
    storyboard.gotoScene( "multiPlayer", "fade", 100 )
	return true	-- indicates successful touch
    end
    
-- 'onPress' event listener for onlineBtn
local function onMultiPlayerBtnPress( )
	multiPlayerBtn.xScale = 1.5; multiPlayerBtn.yScale = 1.5;
	return true	-- indicates successful touch
    end
--
local listenermultiPlayerBtn = function(obj)
    transition.to( multiPlayerBtn, { time = timeMoveToButton, alpha=1.0,x = xPosBtn, y = yPosBtn - 120} )
end    

-- onRelease event listener for optionBtn 
local function onOptionBtnRelease()
	optionBtn.xScale = 1; optionBtn.yScale = 1;
	-- go to level1.lua scene
	--local menu = storyboard.loadScene( "option" )
	--storyboard.gotoScene( "option", "fade", 100 )
	return true	-- indicates successful touch
    end
    
-- 'onPress' event listener for optionBtn
local function onOptionBtnPress( )
	optionBtn.xScale = 1.5; optionBtn.yScale = 1.5;
	return true	-- indicates successful touch
    end
    
-- listenerOptionBtn 
local listenerOptionBtn = function(obj)
    transition.to( optionBtn, { time = timeMoveToButton, alpha = 1.0,x = xPosBtn, y = yPosBtn-80} )
end

-- onRelease event listener for tutorialBt
local function onTutorialBtnRelease()
	tutorialBtn.xScale = 1;tutorialBtn.yScale = 1;
    --local menu = storyboard.loadScene( "tutorial" )
	--storyboard.gotoScene( "tutorial", "fade", 100 )
	return true	-- indicates successful touch
    end
    
-- 'onPress' event listener for tutorialBtn 
local function onTutorialBtnPress()
	tutorialBtn.xScale = 1.5;
	tutorialBtn.yScale = 1.5;
	return true	-- indicates successful touch
    end    
    
-- listenerOnlineBtn 
local listenerTutorialBtn = function(obj)
    transition.to( tutorialBtn, { time = timeMoveToButton, alpha = 1.0,x = xPosBtn, y = yPosBtn - 40} )
end

local tPrevious = system.getTimer()

--Function move map 
local function moveMap(event)
 end

-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
-- 
-- NOTE: Code outside of listener functions (below) will only be executed once,
--		 unless storyboard.removeScene() is called.
-- 
-----------------------------------------------------------------------------------------

-- Called when the scene's view does not exist:
function scene:createScene( event )
	-- display a background image
	     
 
end

-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	local group = self.view
	-- INSERT code here (e.g. start timers, load audio, start listeners, etc.)
	local background = display.newImageRect( "images/background2.png", display.contentWidth, display.contentHeight )
        --local background = display.newImage( "images/menu/backgroundCity.png", true )
	background:setReferencePoint( display.TopLeftReferencePoint  )
	background.x, background.y = 0, 0
	
	local characterBg = display.newImageRect( "images/chipheo.png", display.contentWidth/2, display.contentHeight/2 )
	characterBg:setReferencePoint( display.TopLeftReferencePoint );
	characterBg.width = 100;
	characterBg.height = 100;
	characterBg.x = display.contentWidth/3 ;
	characterBg.y = display.contentHeight/2 ;
    
	-- create a widget button (which will loads level1.lua on release)
	singlePlayerBtn = widget.newButton{
		label = "Single Player",
		labelColor = { default={0}, over={0} },
		--default="images/menu/lr_menu_start.png",
		width = widthButton, height = heightButton,
        onPress = singlePlayerBtnPress,
		onRelease = singlePlayerBtnRelease	-- event listener function
	}
	
	singlePlayerBtn:setReferencePoint( display.CenterReferencePoint )
	singlePlayerBtn.x = xPosBtn
	singlePlayerBtn.y = yPosBtn - 140
    transition.from( singlePlayerBtn, { time = timeMoveButton,  x = xPosBtn, y = 0, onComplete = listenersinglePlayerBtn } )
      	
	multiPlayerBtn = widget.newButton{
		label = "Multiplayer",
		labelColor = { default={0}, over={0} },
		--default="images/menu/lr_menu_start.png",
		width = widthButton, height = heightButton,
        onPress = onMultiPlayerBtnPress,
		onRelease = onMultiPlayerBtnRelease	-- event listener function
	}
	multiPlayerBtn:setReferencePoint( display.CenterReferencePoint )
	multiPlayerBtn.x = xPosBtn
	multiPlayerBtn.y = yPosBtn - 100
    transition.from( multiPlayerBtn, { time = timeMoveButton,  x = xPosBtn, y = 0, onComplete = listenermultiPlayerBtn } )    
    
    -- Create button option
    optionBtn = widget.newButton{
		label = "Option",
		labelColor = { default={0}, over={0} },
		--default="images/menu/lr_menu_options.png",
		width = widthButton, height = heightButton,
        onPress =   onOptionBtnPress,
		onRelease = onOptionBtnRelease 	-- event listener function
	}
	
	optionBtn:setReferencePoint( display.CenterReferencePoint )
	optionBtn.x = xPosBtn
	optionBtn.y = yPosBtn
    transition.from( optionBtn, { time = timeMoveButton,  x = xPosBtn, y = 0, onComplete=listenerOptionBtn } )
    
    -- create button tutorial
    tutorialBtn  = widget.newButton{
		label="Tutorial",
		labelColor = { default={0}, over={0} },
		--default="images/menu/lr_menu_tutorial.png",
		width = widthButton, height = heightButton,
        onPress = onTutorialBtnPress,
		onRelease = onTutorialBtnRelease 	-- event listener function
	}
	tutorialBtn:setReferencePoint( display.CenterReferencePoint )
	tutorialBtn.x = xPosBtn
	tutorialBtn.y = yPosBtn
    transition.from( tutorialBtn, { time = timeMoveButton,  x = xPosBtn, y = 0, onComplete=listenerTutorialBtn } )
        
    -- create button online
    group:insert( background )
    group:insert( characterBg )
    group:insert( optionBtn )
    group:insert( tutorialBtn )
	group:insert( singlePlayerBtn )
	group:insert( multiPlayerBtn )  
end

-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
        print ("hehe");
	-- INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
	if singlePlayerBtn then
		singlePlayerBtn:removeSelf()	-- widgets must be manually removed
		singlePlayerBtn = nil
	end
	if multiPlayerBtn then
		multiPlayerBtn:removeSelf()	-- widgets must be manually removed
		multiPlayerBtn = nil
	end 
    if tutorialBtn then
		tutorialBtn:removeSelf()	-- widgets must be manually removed
		tutorialBtn = nil
    end
    if optionBtn then
		optionBtn:removeSelf()	-- widgets must be manually removed
		optionBtn = nil
    end
    if background then
       background:removeSelf( )
       background = nil
    end         
end

-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
	local group = self.view
	print("remove cac nut bam");
	                                                               
end

-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

-----------------------------------------------------------------------------------------
--Runtime:addEventListener( "enterFrame", moveMap );
return scene

